package com.chapter.sample.redis.mylock;

import cn.hutool.core.util.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.UUID;
import java.util.concurrent.locks.Lock;

@Component
public class DistributedLockFactory {

    @Autowired
    private StringRedisTemplate redisTemplate;

    private String lockName;

    private String uuid;

    public DistributedLockFactory() {
        this.uuid = IdUtil.simpleUUID();
    }

    public Lock getDistributedLock(String lockType) {
        if (lockType == null) {
            return null;
        }
        if (lockType.equalsIgnoreCase("REDIS")) {
            lockName = "redisLock";
            return new RedisDistributedLock(redisTemplate, lockName, uuid);
        } else if (lockType.equalsIgnoreCase("ZOOKEEPER")) {
            //TODO zookeeper版本的分布式锁
            return null;
        } else if (lockType.equalsIgnoreCase("MYSQL")) {
            //TODO MySQL版本的分布式锁
            return null;
        }
        return null;
    }
}
